package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dto.*;
import com.example.entity.Oauth2RegisteredClientEntity;
import com.example.entity.SysStoreEntity;
import com.example.entity.SysUserEntity;
import com.example.exception.BaseException;
import com.example.mybatis.service.impl.CrudServiceImpl;
import com.example.dao.SysTenantDao;
import com.example.entity.SysTenantEntity;
import com.example.service.Oauth2RegisteredClientService;
import com.example.service.SysStoreService;
import com.example.service.SysTenantService;
import com.example.service.SysUserService;
import com.example.utils.ConvertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 租户信息表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 1.0-SNAPSHOT 2025-04-19
 */
@Service
public class SysTenantServiceImpl extends CrudServiceImpl<SysTenantDao, SysTenantEntity, SysTenantDTO> implements SysTenantService {
    @Autowired
    private SysStoreService sysStoreService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private Oauth2RegisteredClientService oauth2RegisteredClientService;

    @Override
    public QueryWrapper<SysTenantEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SysTenantEntity> wrapper = new QueryWrapper<>();

        String name = (String)params.get("name");
        wrapper.eq(StringUtils.isNotBlank(name), "name", name);
        String contactPhone = (String)params.get("contactPhone");
        wrapper.eq(StringUtils.isNotBlank(contactPhone), "contact_phone", contactPhone);
        String status = (String)params.get("status");
        wrapper.eq(StringUtils.isNotBlank(status), "status", status);

        return wrapper;
    }

    @Override
    public void save(SysTenantDTO dto) {
        SysTenantEntity entity = ConvertUtils.sourceToTarget(dto, SysTenantEntity.class);

        baseDao.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTenantStoreUser(SysTenantStoreUserDTO dto) {
        // 保存租户
        SysTenantDTO sysTenantDTO = dto.getSysTenant();
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        long tenantId = snowflake.nextId();
        sysTenantDTO.setTenantId(tenantId);
        this.save(sysTenantDTO);

        List<Long> sysStoreList = dto.getSysStoreList();
        SysStoreEntity sysStoreEntity = new SysStoreEntity();
        sysStoreEntity.setTenantId(tenantId);
        sysStoreService.update(sysStoreEntity, new QueryWrapper<SysStoreEntity>().in("store_id", sysStoreList));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTenantStoreUser(SysTenantStoreUserDTO dto) {
        // 保存租户
        SysTenantDTO sysTenantDTO = dto.getSysTenant();
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        long tenantId = sysTenantDTO.getTenantId();
        sysTenantDTO.setTenantId(tenantId);
        this.update(sysTenantDTO);

        List<Long> sysStoreList = dto.getSysStoreList();
        SysStoreEntity sysStoreEntity = new SysStoreEntity();
        sysStoreEntity.setTenantId(null);
        sysStoreService.update(sysStoreEntity, new QueryWrapper<SysStoreEntity>().eq("tenant_id", tenantId));

        sysStoreEntity.setTenantId(tenantId);
        sysStoreService.update(sysStoreEntity, new QueryWrapper<SysStoreEntity>().in("store_id", sysStoreList));
    }
    @Override
    public SysTenantStoreUserDTO getSysTenantStoreUserDTO(Long id) {

        SysTenantStoreUserDTO sysTenantStoreUserDTO = new SysTenantStoreUserDTO();
        // 租户信息
        SysTenantDTO sysTenantDTO = super.get(id);
        sysTenantStoreUserDTO.setSysTenant(sysTenantDTO);
        List<SysStoreEntity> sysStoreEntities = sysStoreService.selectStoreByTenantId(sysTenantDTO.getTenantId());
        sysTenantStoreUserDTO.setSysStoreList(sysStoreEntities.stream().map(SysStoreEntity::getStoreId).toList());
        return sysTenantStoreUserDTO;
    }

    @Override
    public void deleteTenantStoreUser(Long[] ids) {
        for (Long id : ids) {
            List<SysStoreEntity> sysStoreEntities = sysStoreService.selectStoreByTenantId(id);
            if (CollectionUtil.isNotEmpty(sysStoreEntities)) {
                throw new BaseException("有门店在使用中，请先删除门店");
            }
        }
        super.delete(ids);
    }


}